package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.velocity.anakia.Escape;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Ads;
import com.ejuornal.bean.EditorCommit;
import com.ejuornal.bean.vo.ECUserPeriodical;
import com.ejuornal.dao.EditorCommitVoDao;

@Component("editorCommitVoDao")
public class EditorCommitVoDaoImpl extends DaoSupport<ECUserPeriodical> implements EditorCommitVoDao {

	@Override
	public List<ECUserPeriodical> findAllByEditorChiefId(int editorChief) {
		String sql = "SELECT * from v_ec_user_per where periodicalId in "+
					"(select pp.periodicalId from t_periodical pp where pp.userId = ?)";
		List<ECUserPeriodical> list = super.getJdbcTemplate().query(sql, 
				new Object[]{editorChief}, new ECUserPeriodicalMapper());
		return list;
	}

	@Override
	public List<ECUserPeriodical> findAllByECIdState(int editorChief,int state) {
		String sql ="SELECT * from v_ec_user_per where periodicalId in "+
					"(select pp.periodicalId from t_periodical pp where pp.userId = ?) and "+
					"v_ec_user_per.result =?";   //未审核为0，审核为1
		List<ECUserPeriodical> list = super.getJdbcTemplate().query(sql, 
				new Object[]{editorChief,state}, new ECUserPeriodicalMapper());
		return list;
	}
	
	@Override
	public ECUserPeriodical getEditorCommitInfoById(int editorCommitId) {
		String sql = "select * from v_ec_user_per where editorCommitId = ?";
		ECUserPeriodical editorCommit = super.getJdbcTemplate().queryForObject(sql, 
				new Object[]{editorCommitId}, 
				new ECUserPeriodicalMapper());
		return editorCommit;
	}
	
	@Override
	public List<ECUserPeriodical> findAllEditorCommitBiPerId(int periodicalId) {
		String sql = "select * from v_ec_user_per where periodicalId = ?";
		List<ECUserPeriodical> list = super.getJdbcTemplate().query(sql, 
				new Object[]{periodicalId}, new ECUserPeriodicalMapper());
		return list;
	}
	
	final class ECUserPeriodicalMapper implements RowMapper<ECUserPeriodical> {
		public ECUserPeriodical mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			ECUserPeriodical editorChiefExt = new ECUserPeriodical();
			editorChiefExt.setUserName(rs.getString("userName"));
			editorChiefExt.setUserId(rs.getInt("userId"));
			editorChiefExt.setTel(rs.getString("tel"));    
			editorChiefExt.setSex(rs.getString("sex"));
			editorChiefExt.setRealName(rs.getString("realName"));
			editorChiefExt.setName(rs.getString("name"));
			editorChiefExt.setHostUnit(rs.getString("hostUnit"));
			editorChiefExt.setEmail(rs.getString("email"));
			editorChiefExt.setReason(rs.getString("reason"));
			editorChiefExt.setCompany(rs.getString("company"));
			editorChiefExt.setApplyDate(rs.getDate("applyDate"));
			editorChiefExt.setEditorCommitId(rs.getInt("editorCommitId"));
			editorChiefExt.setPeriodicalId(rs.getInt("periodicalId"));
			editorChiefExt.setResult(rs.getString("result"));
			return editorChiefExt;
		}
	}

}
